<?php 
namespace Common\Model;
use Think\Model;
class ColumnsModel extends Model{
	protected $table;
	protected $pre;
	protected $colutable;

	public function __construct($colutable,$pre =''){
		parent::__construct();
		$this->pre = empty($pre) ? C('DB_PREFIX') : $pre;
		$this->table = $colutable;
		$this->goodstable = $this -> $pre.$colutable;
	}

	public function addCol($data){
		$checkInfo = $this -> checkInfo($data);
		if($checkInfo !== true){
			return $checkInfo;
		}
		$coldata = $this -> formatInfo($data);
		$colus = M($this->table);
		$cid = $colus -> add($coldata,[],true);
		if(!$cid){
			return $this -> returnInfo('','失败了');
		}
		return $this -> returnInfo('','成功了',1);
	}

	public function checkInfo($data){
		if(empty($data['title'])){
			return $this -> returnInfo('','栏目名称不能为空！');
		}
		if(empty($data['entitle'])){
			return $this -> returnInfo('','英文名称不能为空！');
		}
		if(empty($data['type'])){
			return $this -> returnInfo('','请选择类型！');
		}
		if(strlen($data['sort']) > 6 ){
			return $this -> returnInfo('','排序不符合规则！');
		}	

		if(empty($data['id'])){
			$coluCount = M($this->table) -> field('id') -> where(['coltitle' => $data['title']]) -> count();
			if($coluCount > 0){
				return $this -> returnInfo('','栏目名称不能重复！');
			}
		}
		return true;
	}

	public function formatInfo($data){
		$id = empty($data['id']) ? 0 : $data['id'];
		if(empty($id)){
			$pid = empty($data['pid']) ? 0 : $data['pid'];
		}else{
			$pid = empty($data['editpid']) ? 0 : $data['editpid'];
		}
		$seo_title = empty($data['seo_title']) ? '' : $data['seo_title'];
		$seo_keywords = empty($data['seo_keywords']) ? '' : $data['seo_keywords'];
		$seo_description = empty($data['seo_description']) ? '' : $data['seo_description'];
		$is_nav = empty($data['is_nav']) ? 1 : $data['is_nav'];
		$sort = empty($data['sort']) ? 0 :$data['sort'];
		$status = empty($data['status']) ? 0 : $data['status'];
		$dataInfo = ['id' => $id,'pid' => $pid,'coltitle' => $data['title'],'type'=>$data['type'],'entitle' => $data['entitle'],'seo_title' => $seo_title,'seo_keywords' => $seo_keywords,'seo_description' => $seo_description,'is_nav'=> $is_nav,'sort' => $sort,'status' => $status];
		return $dataInfo;

	}	

	public function returnInfo($data,$msg = '',$status = 0){
		return ['status' => $status, 'mess' => $msg, 'data' => $data];
	}

	public function colulist($type,$field = 'id',$order = 'id asc'){
		$colu = M($this->table);
		$list = $colu -> field($field);
		if(!empty($type)){
			$list = $list -> where(['type' => $type]);
		}
		$list = $list -> order($order) -> select();
		if(empty($list)){
			return $this -> returnInfo('','没有数据',0);
		}else{
			return $this -> returnInfo($list,'成功展示',1);
		}
	}

	public function editlist($id,$field = 'id',$order = 'id asc'){
		$colu = M($this->table);
		$list = $colu -> field($field);
		if(!empty($id)){
			$list = $list -> where(['id' => $id]);
		}
		$list = $list -> order($order) -> select();

		if(empty($list)){
			return $this -> returnInfo('','没有数据',0);
		}else{
			return $this -> returnInfo($list,'成功展示',1);
		}
	}

	public function Delcolu($id){
		if(empty($id)){
			return $this -> returnInfo('','操作异常！');
		}
		$colu_childnum = M($this->table) -> where(['pid' => $id]) -> count();
		if($colu_childnum > 0){
			return $this -> returnInfo('','请先删除子栏目');
		}
		$is_del = M($this->table) -> where(['id' => $id]) -> delete();
		if(!$is_del){
			return $this -> returnInfo('','删除失败');
		}
		return $this -> returnInfo('','删除成功！',1);

	}

	public function Alterstate($id){
		if(empty($id)){
			return $this -> returnInfo('','修改异常');
		}
		$coluInfo = M($this->table) -> field('status') -> where(['id' => $id]) -> find();
		$result = [];
		if($coluInfo['status'] == 0){
			M($this->table) -> where(['id' => $id]) -> setField(['status' => 1]);
			$result = $this -> returnInfo(1,'已开启',1);
		}else{
			M($this->table) -> where(['id' => $id]) -> setField(['status' => 0]);
			$result = $this -> returnInfo(0,'已禁用',1);
		}
		return $result;
	}


}